﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.sale
{
    public partial class customer_add : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        private void Reset()
        {
            txt_customername.Text = "";
            txt_address.Text = "";
            txt_contact.Text = "";
            txt_mobile.Text = "";
            txt_email.Text = "";
            txt_phone.Text = "";
            txt_fax.Text = "";
            txt_qq.Text = "";

            uc_from_city.Reset();
            uc_from_settle.Reset();
            uc_from_credit.Reset();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            edowork.com.Model.customerInfo customerInfo = new customerInfo();

            customerInfo.Customername = txt_customername.Text.Trim();
            customerInfo.Cityid = uc_from_city.getValue();
            customerInfo.Address = txt_address.Text.Trim();
            customerInfo.Contact = txt_contact.Text.Trim();
            customerInfo.Mobile = txt_mobile.Text.Trim();
            customerInfo.Email = txt_email.Text.Trim();
            customerInfo.Phone = txt_phone.Text.Trim();
            customerInfo.Fax = txt_fax.Text.Trim();
            customerInfo.Qq = txt_qq.Text.Trim();
            customerInfo.Settleid = uc_from_settle.getValue();
            customerInfo.Creditid = uc_from_credit.getValue();

            customerInfo.Sysdate = DateTime.Now.ToString("yyyy-MM-dd");

            if (String.IsNullOrEmpty(customerInfo.Customername))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入客户名称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_customername.Focus();

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Address))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入详细地址");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_address.Focus();

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Contact))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入联系人");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_contact.Focus();

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Settleid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择结算方式");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Creditid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择信用等级");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //取得客户编码
            customerInfo.Customerid = SysID.SCR_CustomerID(Session["connectionString"].ToString(), ref alert_msg);

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入customer表
                sql = String.Format(customerInfo.SQL_Insert(), customerInfo.Customerid, customerInfo.Customername,
                                                               customerInfo.Cityid, customerInfo.Address, customerInfo.Contact,
                                                               customerInfo.Mobile, customerInfo.Email, customerInfo.Phone,
                                                                customerInfo.Fax, customerInfo.Qq, customerInfo.Settleid, customerInfo.Creditid, customerInfo.Sysdate);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            Reset();

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //重置
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/sale/customer.aspx");
        }
    }
}